package org.duanyu.controller;

import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatOptions;
import com.alibaba.cloud.ai.dashscope.chat.MessageFormat;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.model.Media;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


/**
 * 图生文 图像理解
 */
@RestController
public class ImageToTextController {

    @Autowired
    private ChatModel chatModel;

    /**
     * 图生文
     * @param file 文件
     * @param message 提示词
     * @return
     */
    @PostMapping("/image")
    public String imageToText(MultipartFile file, String message){

        //创建媒体
        Media media = new Media(MimeTypeUtils.IMAGE_PNG,file.getResource());

        //创建用户消息
        UserMessage userMessage = new UserMessage(message,media);
        userMessage.getMetadata().put(DashScopeChatModel.MESSAGE_FORMAT, MessageFormat.IMAGE);
        //发送给LLM
        ChatResponse chatResponse = chatModel.call(new Prompt(userMessage, DashScopeChatOptions.builder()
                .withModel("qwen-vl-max-latest")
                .withMultiModel(true).build()));
        return chatResponse.getResult().getOutput().getContent();

    }
}
